<?php
/**
 * report.php 数据服务类
 */

class QR {

	public static function  createQRCode($text, $fname=false) {
		$PNG_TEMP_DIR = ATTACHMENT_PATH.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
		$FILEEXT = '.png';
		if(empty($text)) return false;
		if(!self::isHasQRCode($fname)) {
			if($fname===false) $fname=time();
			include_once(VENDOR_PATH."phpqrcode/qrlib.php");
			//ofcourse we need rights to create temp dir
			if (!file_exists($PNG_TEMP_DIR)) {mkdir($PNG_TEMP_DIR);}
			$filename = $PNG_TEMP_DIR . $fname . $FILEEXT;
			$errorCorrectionLevel = 'L'; // 'L','M','Q','H'
			$matrixPointSize = 10; // 1-10 也许
			QRcode::png($text, $filename, $errorCorrectionLevel, $matrixPointSize, 2); 
		}
		return SERVER_URL . '/attachments/temp/' . $fname  . $FILEEXT;
	}

	private static function isHasQRCode($fname=false) {
		$PNG_TEMP_DIR = ATTACHMENT_PATH.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
		$FILEEXT = '.png';
		if ($fname===false) return false;
		return file_exists($PNG_TEMP_DIR.$fname.'.png');
	}
}
?>
